/*
 * SimChewieView.java
 */
package simchewie;

import java.awt.BasicStroke;
import simchewie.algorithms.ImmerRechts;
import simchewie.algorithms.ImmerRechtsMitBrain;
import simchewie.algorithms.Wookie;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;

import config.*;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileFilter;
import simchewie.algorithms.ImmerRechtsBWB;
import simchewie.algorithms.STPNavigator;

/**
 * The application's main frame.
 */
public class SimChewieView extends FrameView {

    private boolean loading = false;
    Labyrinth fields = new Labyrinth();
    int ix = 0;
    int iy = 0;
    int rotation = 0;
    int consoleline = 0;
    int selectedfield = 0;
    boolean wookiefree = false;
    Wookie wookie = null;
    ArrayList<Wookie> alist = new ArrayList<Wookie>();
    int steps = 0;
    boolean loaded = false;
    File file = null;

    public SimChewieView(SingleFrameApplication app) {
        super(app);

        initComponents();
        addWookies();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }

            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = SimChewieApp.getApplication().getMainFrame();
            aboutBox = new SimChewieAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        SimChewieApp.getApplication().show(aboutBox);
    }

    public void addWookies() {
        alist.clear();
        //Algorithmen

        addListItem("immer Rechts", new ImmerRechts(fields, 1, 13), true);
        addListItem("immer Rechts mit Erinnerung", new ImmerRechtsMitBrain(fields, 1, 13), false);
        //addListItem("immer Rechts BWB", new ImmerRechtsBWB(fields, 1, 12), false);
        addListItem("Patricks Navigator", new STPNavigator(fields, 1, 13), false);
        if (jList1.getSelectedIndex() == -1) {
            jList1.setSelectedIndex(0);
        }
    }

    public void addListItem(String name, Wookie algorithm, boolean clear) {
        DefaultListModel listmodel = (DefaultListModel) jList1.getModel();
        if (clear) {
            listmodel.clear();
        }
        listmodel.addElement(name);
        alist.add(algorithm);
    }

    public Graphics drawCanvas() {



        Graphics g = jPanel3.getGraphics();
        jPanel3.paintComponents(g);

        Graphics2D g2 = (Graphics2D) g;
        g2.setBackground(Color.white);
        
        g2.clearRect(0, 0, 400 + 80, 480 + 80);
        for (int i = 0; i < 13 + 2; i++) {
            g.drawLine(0, i * 40, 400 + 80, i * 40);
        }

        for (int i = 0; i < 11 + 2; i++) {
            g.drawLine(i * 40, 0, i * 40, 480 + 80);
        }

        
        String res = "/simchewie/resources/";
        Image img = null;
        Field f = null;
        int c = 0;
        int rot = 0;
        for (int i = 0; i < 10 + 2; i++) {
            for (int j = 0; j < 12 + 2; j++) {


                res = "/simchewie/resources/";
                f = fields.getField(i, j);
                c = fields.countField(i, j);
                if (c > 0) {
                    if (c == 1) {

                        res += "sackgasse.jpg";
                        if (f.north) {
                            rot = 90;
                        } else if (f.south) {
                            rot = 270;
                        } else if (f.west) {
                            rot = 0;
                        } else if (f.east) {
                            rot = 180;
                        }
                    } else if (c == 2) {

                        if ((f.north && f.south) || (f.east && f.west)) {
                            res += "gerade.jpg";

                            if (f.north) {
                                rot = 90;
                            } else if (f.south) {
                                rot = 270;
                            } else if (f.west) {
                                rot = 0;
                            } else if (f.east) {
                                rot = 180;
                            }
                        } else {
                            res += "kurve.jpg";
                            if (f.north) {
                                if (f.east) {
                                    rot = 0;
                                } else {
                                    rot = 270;
                                }

                            } else {
                                if (f.east) {
                                    rot = 90;
                                } else {
                                    rot = 180;
                                }
                            }

                        }
                    } else if (c == 3) {

                        res += "tstueck.jpg";
                        if (!f.north) {
                            rot = 0;
                        } else if (!f.south) {
                            rot = 180;
                        } else if (!f.west) {
                            rot = 270;
                        } else if (!f.east) {
                            rot = 90;
                        }
                    } else if (c == 4) {
                        res += "kreuzung.jpg";
                        rot = 0;
                    }
                    img = Toolkit.getDefaultToolkit().getImage(getClass().getResource(res));
                    BufferedImage bimg = Pictures.toBufferedImage(img);
                    bimg = Pictures.rotateImage(bimg, rot);
                    g.drawImage(bimg, i * 40 + 1, j * 40 + 1, null);
                }
            }
        }
        if (fields.isFinished()) {
            g.setColor(Color.red);
            g.fillOval(fields.finx * 40 + 12, fields.finy * 40 + 12, 15, 15);
        }

        g2.setColor(Color.black);
        g2.setStroke(new BasicStroke(2));
        g2.drawLine(40, 520, 40, 560 - ((Config.MAXFIELDS-1) * 40));
        g2.drawLine((Config.MINFIELDS-1) * 40, 520, (Config.MINFIELDS -1) * 40, 560 - ((Config.MAXFIELDS-1) * 40));
        g2.drawLine(40, 520, (Config.MINFIELDS-1) * 40 , 520);
        g2.drawLine(40, 560 - ((Config.MAXFIELDS-1) * 40), (Config.MINFIELDS-1) * 40 , 560 - ((Config.MAXFIELDS-1) * 40));
        return g;
    }

    public void drawWookie() {

        Graphics g = drawCanvas();
        Image img = null;
        Field f = null;
        int c = 0;
        int rot = 0;
        String res = "/simchewie/resources/";


        
        Point p = wookie.getPosition();
        rot = wookie.getOrientation();


        res = "/simchewie/resources/";

        if (rot == 0) {
            res += "chewie.png";
        } else if (rot == 1) {
            res += "chewie90.png";
        } else if (rot == 2) {
            res += "chewie180.png";
        } else if (rot == 3) {
            res += "chewie270.png";
        }

        img = Toolkit.getDefaultToolkit().getImage(getClass().getResource(res));
        BufferedImage bimg = Pictures.toBufferedImage(img);
        //bimg = Pictures.rotateImage(bimg, rot);
        g.drawImage(bimg, p.x * 40 + 1, p.y * 40 + 1, null);
        //drawStraight(0, 0);
        //drawStraight(1,0);



        //g2.drawLine(0,20,20,0);

    }

    public void drawWookieBrain() {
        if (jFrame1.isVisible()) {


            Graphics g = jPanel5.getGraphics();
            jPanel5.paintComponents(g);

            Graphics2D g2 = (Graphics2D) g;
            g2.setBackground(Color.white);
            g2.clearRect(0, 0, 400 + 80, 480 + 80);
            for (int i = 0; i < 13; i++) {
                g.drawLine(0, i * 40, 400 + 80, i * 40);
            }

            for (int i = 0; i < 11; i++) {
                g.drawLine(i * 40, 0, i * 40, 480 + 80);
            }

            String res = "/simchewie/resources/";
            Image img = null;
            Field f = null;
            int c = 0;
            int rot = 0;
            boolean[][] washere = wookie.getWasHere();
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 12; j++) {


                    res = "/simchewie/resources/";
                    f = fields.getField(i, j);
                    c = fields.countField(i, j);
                    if (c > 0) {
                        if (c == 1) {

                            res += "sackgasse.jpg";
                            if (f.north) {
                                rot = 90;
                            } else if (f.south) {
                                rot = 270;
                            } else if (f.west) {
                                rot = 0;
                            } else if (f.east) {
                                rot = 180;
                            }
                        } else if (c == 2) {

                            if ((f.north && f.south) || (f.east && f.west)) {
                                res += "gerade.jpg";

                                if (f.north) {
                                    rot = 90;
                                } else if (f.south) {
                                    rot = 270;
                                } else if (f.west) {
                                    rot = 0;
                                } else if (f.east) {
                                    rot = 180;
                                }
                            } else {
                                res += "kurve.jpg";
                                if (f.north) {
                                    if (f.east) {
                                        rot = 0;
                                    } else {
                                        rot = 270;
                                    }

                                } else {
                                    if (f.east) {
                                        rot = 90;
                                    } else {
                                        rot = 180;
                                    }
                                }

                            }


                        } else if (c == 3) {

                            res += "tstueck.jpg";
                            if (!f.north) {
                                rot = 0;
                            } else if (!f.south) {
                                rot = 180;
                            } else if (!f.west) {
                                rot = 270;
                            } else if (!f.east) {
                                rot = 90;
                            }
                        } else if (c == 4) {
                            res += "kreuzung.jpg";
                            rot = 0;
                        }
                        img = Toolkit.getDefaultToolkit().getImage(getClass().getResource(res));
                        BufferedImage bimg = Pictures.toBufferedImage(img);
                        bimg = Pictures.rotateImage(bimg, rot);
                        g.drawImage(bimg, i * 40 + 1, j * 40 + 1, null);
                        if (washere[i][j]) {
                            g.setColor(Color.red);
                            g.drawRect(i * 40 + 1, j * 40 + 1, 39, 39);
                        }
                    }
                }


            }

            if (fields.isFinished()) {
                g.setColor(Color.red);
                g.fillOval(fields.finx * 40 + 12, fields.finy * 40 + 12, 15, 15);
            }

            Point p = wookie.getPosition();
            rot = wookie.getOrientation();


            res = "/simchewie/resources/";

            if (rot == 0) {
                res += "chewie.png";
            } else if (rot == 1) {
                res += "chewie90.png";
            } else if (rot == 2) {
                res += "chewie180.png";
            } else if (rot == 3) {
                res += "chewie270.png";
            }

            img = Toolkit.getDefaultToolkit().getImage(getClass().getResource(res));
            BufferedImage bimg = Pictures.toBufferedImage(img);
            //bimg = Pictures.rotateImage(bimg, rot);
            g.drawImage(bimg, p.x * 40 + 1, p.y * 40 + 1, null);
            //drawStraight(0, 0);
            //drawStraight(1,0);



            //g2.drawLine(0,20,20,0);
        }
    }

    void addConsole(String s) {
        String tmp = jTextArea1.getText();
        jTextArea1.setText(tmp + "\n" + consoleline++ + "\t" + s);


    }

    public void setWookieFree(Wookie wook) {
        wookie = wook;
        steps = 0;

        drawWookie();


    }

    public void nextStep() {
        if (loaded) {
            if (!wookie.isFinished()) {
                wookie.nextstep();
                addConsole(
                        "Position: " + wookie.getPosition().x + ", " + wookie.getPosition().y);
                addConsole(
                        "Orientation: " + wookie.getOrientation());
                drawWookie();
                drawWookieBrain();
                steps++;

            } else {
                addConsole("Finished! Wookie needed " + steps + " steps");


            }
        } else {
            addConsole("FAIL No Algorithm loaded!");


        }
    }

    public void runTillEnd() {
        if (loaded) {
            if (fields.isFinished()) {
                addConsole("Wookie running till end...");


                while (!wookie.isFinished()) {
                    wookie.nextstep();
                    //drawWookie();
                    steps++;
                    if (steps > 1000) {
                        addConsole("Wokiee needed too much steps");
                        break;
                    }
                }


                drawWookie();
                drawWookieBrain();
                addConsole(
                        "Wookie needed " + steps + " steps");


            } else {
                addConsole("FAIL Labyrinth has no Finish!");


            }

        } else {
            addConsole("FAIL No Algorithm loaded!");


        }
    }

    @Action
    public void loadFile() {

        int returnVal = jFileChooser1.showOpenDialog(jLabel1);


        if (returnVal == JFileChooser.APPROVE_OPTION) {
            //Initialise file object
            file = jFileChooser1.getSelectedFile();
            fields.load(file);
            drawCanvas();
            addConsole(
                    "File loaded!");


        }


    }

    @Action
    public void newFile() {
        file = null;
        fields = new Labyrinth();
        drawCanvas();


    }

    @Action
    public void saveFile() {
        if (file == null) {
            int returnVal = jFileChooser1.showSaveDialog(jLabel1);


            if (returnVal == JFileChooser.APPROVE_OPTION) {
                //Initialise file object
                file = jFileChooser1.getSelectedFile();
                fields.save(file);
                addConsole(
                        "File saved!");


            }

        } else {
            fields.save(file);
            addConsole(
                    "File saved!");


        }
        drawCanvas();


    }

    @Action
    public void saveAs() {
        int returnVal = jFileChooser1.showSaveDialog(jLabel1);


        if (returnVal == JFileChooser.APPROVE_OPTION) {
            //Initialise file object
            file = jFileChooser1.getSelectedFile();
            fields.save(file);
            addConsole(
                    "File saved!");
            drawCanvas();


        }
    }

    void runAll() {
        if (fields.isFinished()) {
            int i = 0;
            addWookies();


            for (Wookie w : alist) {
                wookie = w;
                steps = 0;
                jList1.setSelectedIndex(i);
                addConsole(
                        "\"" + jList1.getSelectedValue() + "\" loaded and runnig till end...");


                while (!wookie.isFinished()) {
                    wookie.nextstep();
                    //drawWookie();
                    steps++;
                    if (steps > 1000) {
                        addConsole(jList1.getSelectedValue() + "needed too much steps");
                        break;
                    }

                }


                drawWookie();
                jList1.setSelectedIndex(i);
                addConsole(
                        "\"" + jList1.getSelectedValue() + "\" needed " + steps + " steps!");
                i++;

            }


        } else {
            addConsole("FAIL Labyrinth has no Finish!");


        }
    }

    public void drawPreview(int x, int y, int rota) {


        Graphics g = drawCanvas();

        if (selectedfield != 0 && selectedfield != 5) {
            Image imgg = null;
            String ress = "/simchewie/resources/";


            if (selectedfield == 1) {
                ress += "gerade.jpg";


            } else if (selectedfield == 2) {
                ress += "tstueck.jpg";


            } else if (selectedfield == 3) {
                ress += "kreuzung.jpg";


            } else if (selectedfield == 4) {
                ress += "leer.jpg";


            } else if (selectedfield == 6) {
                ress += "kurve.jpg";
            }
            imgg = Toolkit.getDefaultToolkit().getImage(getClass().getResource(ress));
            BufferedImage bimgg = Pictures.toBufferedImage(imgg);
            bimgg = Pictures.rotateImage(bimgg, rota);
            g.drawImage(bimgg, x * 40 + 1, y * 40 + 1, null);


        }


        //drawStraight(0, 0);
        //drawStraight(1,0);



        //g2.drawLine(0,20,20,0);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        jFileChooser1 = new javax.swing.JFileChooser();
        jFrame1 = new javax.swing.JFrame();
        jPanel6 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();

        mainPanel.setName("mainPanel"); // NOI18N

        jPanel3.setBackground(Color.white);
        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setPreferredSize(new java.awt.Dimension(481, 561));
        jPanel3.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                jPanel3MouseWheelMoved(evt);
            }
        });
        jPanel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jPanel3MouseReleased(evt);
            }
        });
        jPanel3.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                jPanel3MouseMoved(evt);
            }
        });
        jPanel3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jPanel3KeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 481, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 561, Short.MAX_VALUE)
        );

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N
        jTabbedPane1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jTabbedPane1StateChanged(evt);
            }
        });

        jPanel4.setName("jPanel4"); // NOI18N

        jLabel1.setBackground(Color.white);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simchewie/resources/gerade.jpg")));
        jLabel1.setText("Gerade");
        jLabel1.setMinimumSize(new java.awt.Dimension(6, 14));
        jLabel1.setName("jLabel1"); // NOI18N
        jLabel1.setOpaque(true);
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel1MouseClicked(evt);
            }
        });

        jLabel2.setBackground(Color.white);
        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simchewie/resources/tstueck.jpg")));
        jLabel2.setText("T-Stueck");
        jLabel2.setName("jLabel2"); // NOI18N
        jLabel2.setOpaque(true);
        jLabel2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel2MouseClicked(evt);
            }
        });

        jLabel3.setBackground(Color.white);
        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simchewie/resources/kreuzung.jpg")));
        jLabel3.setText("Kreuzung");
        jLabel3.setName("jLabel3"); // NOI18N
        jLabel3.setOpaque(true);
        jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel3MouseClicked(evt);
            }
        });

        jLabel4.setBackground(Color.white);
        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simchewie/resources/leer.jpg")));
        jLabel4.setText("leer");
        jLabel4.setName("jLabel4"); // NOI18N
        jLabel4.setOpaque(true);
        jLabel4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel4MouseClicked(evt);
            }
        });

        jLabel5.setBackground(Color.white);
        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simchewie/resources/ziel.jpg")));
        jLabel5.setText("Ziel");
        jLabel5.setName("jLabel5"); // NOI18N
        jLabel5.setOpaque(true);
        jLabel5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel5MouseClicked(evt);
            }
        });

        jLabel8.setBackground(Color.white);
        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simchewie/resources/kurve.jpg")));
        jLabel8.setText("Kurve");
        jLabel8.setMinimumSize(new java.awt.Dimension(6, 14));
        jLabel8.setName("jLabel8"); // NOI18N
        jLabel8.setOpaque(true);
        jLabel8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel8MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(160, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Level Editor", jPanel4);

        jPanel2.setMaximumSize(new java.awt.Dimension(32767, 480));
        jPanel2.setMinimumSize(new java.awt.Dimension(0, 480));
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setPreferredSize(new java.awt.Dimension(381, 480));

        jLabel7.setBackground(Color.white);
        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simchewie/resources/end.png")));
        jLabel7.setText("run till end");
        jLabel7.setName("jLabel7"); // NOI18N
        jLabel7.setOpaque(true);
        jLabel7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel7MouseClicked(evt);
            }
        });

        jLabel10.setBackground(Color.white);
        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simchewie/resources/load.png")));
        jLabel10.setText("load");
        jLabel10.setName("jLabel10"); // NOI18N
        jLabel10.setOpaque(true);
        jLabel10.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel10MouseClicked(evt);
            }
        });

        jLabel6.setBackground(Color.white);
        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simchewie/resources/next.png")));
        jLabel6.setText("next step");
        jLabel6.setName("jLabel6"); // NOI18N
        jLabel6.setOpaque(true);
        jLabel6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel6MouseClicked(evt);
            }
        });

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel9.setText("Algorithmen");
        jLabel9.setName("jLabel9"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jList1.setModel(new DefaultListModel());
        jList1.setName("jList1"); // NOI18N
        jList1.setSelectedIndex(0);
        jScrollPane2.setViewportView(jList1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel9)
                .addContainerGap(162, Short.MAX_VALUE))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        jLabel11.setBackground(Color.white);
        jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simchewie/resources/runall.png")));
        jLabel11.setText("run all");
        jLabel11.setName("jLabel11"); // NOI18N
        jLabel11.setOpaque(true);
        jLabel11.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel11MouseClicked(evt);
            }
        });

        jLabel12.setBackground(Color.white);
        jLabel12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simchewie/resources/runall.png")));
        jLabel12.setText("show brain");
        jLabel12.setName("jLabel12"); // NOI18N
        jLabel12.setOpaque(true);
        jLabel12.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel12MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
            .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Simulation", jPanel2);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 729, Short.MAX_VALUE)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 481, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText("File");
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(simchewie.SimChewieApp.class).getContext().getActionMap(SimChewieView.class, this);
        jMenuItem4.setAction(actionMap.get("newFile")); // NOI18N
        jMenuItem4.setText("New...");
        jMenuItem4.setName("jMenuItem4"); // NOI18N
        fileMenu.add(jMenuItem4);

        jMenuItem2.setAction(actionMap.get("loadFile")); // NOI18N
        jMenuItem2.setText("Open...");
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        fileMenu.add(jMenuItem2);

        jMenuItem1.setAction(actionMap.get("saveFile")); // NOI18N
        jMenuItem1.setText("Save");
        jMenuItem1.setAutoscrolls(true);
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        fileMenu.add(jMenuItem1);

        jMenuItem3.setAction(actionMap.get("saveAs")); // NOI18N
        jMenuItem3.setText("Save As...");
        jMenuItem3.setName("jMenuItem3"); // NOI18N
        fileMenu.add(jMenuItem3);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(simchewie.SimChewieApp.class).getContext().getResourceMap(SimChewieView.class);
        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 567, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        jFileChooser1.setBackground(resourceMap.getColor("jFileChooser1.background")); // NOI18N
        jFileChooser1.setCurrentDirectory(null);
        jFileChooser1.setFileFilter(new FileFilter() {
            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".clab") || f.isDirectory();
            }
            public String getDescription() {
                return "Chewie-Labyrinth(*.clab)";
            }
        });
        jFileChooser1.setName("jFileChooser1"); // NOI18N

        jFrame1.setMinimumSize(new java.awt.Dimension(430, 510));
        jFrame1.setName("jFrame1"); // NOI18N

        jPanel6.setName("jPanel6"); // NOI18N

        jPanel5.setBackground(Color.white);
        jPanel5.setMinimumSize(new java.awt.Dimension(401, 481));
        jPanel5.setName("jPanel5"); // NOI18N
        jPanel5.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                jPanel5MouseWheelMoved(evt);
            }
        });
        jPanel5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jPanel5MouseReleased(evt);
            }
        });
        jPanel5.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                jPanel5MouseMoved(evt);
            }
        });
        jPanel5.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jPanel5KeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 401, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 481, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 401, Short.MAX_VALUE)
            .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel6Layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 481, Short.MAX_VALUE)
            .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel6Layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void jLabel3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseClicked
        if (selectedfield == 3) {
            jLabel3.setBackground(Color.white);
            selectedfield = 0;


        } else {
            selectedfield = 3;
            jLabel1.setBackground(Color.white);
            jLabel2.setBackground(Color.white);
            jLabel3.setBackground(Color.pink);
            jLabel4.setBackground(Color.white);
            jLabel5.setBackground(Color.white);
            jLabel8.setBackground(Color.white);

        }

    }//GEN-LAST:event_jLabel3MouseClicked

    private void jLabel4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel4MouseClicked
        if (selectedfield == 4) {
            jLabel4.setBackground(Color.white);
            selectedfield = 0;


        } else {
            selectedfield = 4;
            jLabel1.setBackground(Color.white);
            jLabel2.setBackground(Color.white);
            jLabel3.setBackground(Color.white);
            jLabel4.setBackground(Color.pink);
            jLabel5.setBackground(Color.white);
            jLabel8.setBackground(Color.white);


        }

    }//GEN-LAST:event_jLabel4MouseClicked

    private void jLabel5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel5MouseClicked
        if (selectedfield == 5) {
            jLabel5.setBackground(Color.white);
            selectedfield = 0;


        } else {
            selectedfield = 5;
            jLabel1.setBackground(Color.white);
            jLabel2.setBackground(Color.white);
            jLabel3.setBackground(Color.white);
            jLabel4.setBackground(Color.white);
            jLabel5.setBackground(Color.pink);
            jLabel8.setBackground(Color.white);


        }

    }//GEN-LAST:event_jLabel5MouseClicked

    private void jLabel6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel6MouseClicked
        nextStep();
    }//GEN-LAST:event_jLabel6MouseClicked
    private void jLabel7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel7MouseClicked
        runTillEnd();
    }//GEN-LAST:event_jLabel7MouseClicked
    private void jLabel10MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel10MouseClicked
        // TODO add your handling code here:
        loadWookie();

    }//GEN-LAST:event_jLabel10MouseClicked

    private void jLabel11MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel11MouseClicked
        if (loaded) {
            loaded = false;
            jLabel10.setBackground(Color.white);
            addWookies();
            addConsole(
                    "Algorithm dismounted!");


        }
        runAll();

    }//GEN-LAST:event_jLabel11MouseClicked

    private void jPanel3KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPanel3KeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
            rotation = (rotation + 90) % 360;
            addConsole(
                    "rotate" + rotation);
            drawPreview(
                    ix, iy, rotation);



        }

        if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
            rotation = (rotation - 90) % 360;


            if (rotation < 0) {
                rotation = 360 + rotation;


            }
            addConsole("rotate" + rotation);
            drawPreview(
                    ix, iy, rotation);



        }
    }//GEN-LAST:event_jPanel3KeyReleased

    private void jPanel3MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel3MouseReleased

        int x = (int) Math.floor(evt.getPoint().x / 40);


        int y = (int) Math.floor(evt.getPoint().y / 40);



        if (selectedfield == 1) {
            if (rotation == 90 || rotation == 270) {
                fields.setField(x, y, "north", true);
                fields.setField(x, y, "south", true);


            } else {
                fields.setField(x, y, "east", true);
                fields.setField(x, y, "west", true);


            }
        } else if (selectedfield == 2) {
            if (rotation == 0) {


                fields.setField(x, y, "east", true);
                fields.setField(x, y, "west", true);
                fields.setField(x, y, "south", true);


            } else if (rotation == 90) {
                fields.setField(x, y, "north", true);
                fields.setField(x, y, "south", true);

                fields.setField(x, y, "west", true);


            } else if (rotation == 180) {


                fields.setField(x, y, "east", true);
                fields.setField(x, y, "west", true);
                fields.setField(x, y, "north", true);


            } else if (rotation == 270) {
                fields.setField(x, y, "north", true);
                fields.setField(x, y, "south", true);
                fields.setField(x, y, "east", true);


            }
        } else if (selectedfield == 3) {
            fields.setField(x, y, "north", true);
            fields.setField(x, y, "south", true);
            fields.setField(x, y, "east", true);
            fields.setField(x, y, "west", true);
            drawCanvas();


        } else if (selectedfield == 4) {
            fields.setField(x, y, "north", false);
            fields.setField(x, y, "south", false);
            fields.setField(x, y, "east", false);
            fields.setField(x, y, "west", false);
            drawCanvas();


        } else if (selectedfield == 5) {
            fields.setFinish(x, y);


        } else if (selectedfield == 6) {
            if (rotation == 0) {


                fields.setField(x, y, "north", true);
                fields.setField(x, y, "east", true);



            } else if (rotation == 90) {
                fields.setField(x, y, "east", true);
                fields.setField(x, y, "south", true);




            } else if (rotation == 180) {


                fields.setField(x, y, "south", true);
                fields.setField(x, y, "west", true);



            } else if (rotation == 270) {
                fields.setField(x, y, "north", true);
                fields.setField(x, y, "west", true);



            }


        }

        if (selectedfield < 6) {
            drawCanvas();


        }
        addConsole("eingetragen");

    }//GEN-LAST:event_jPanel3MouseReleased
    private void jPanel3MouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel3MouseMoved

        if (selectedfield != 0) {
            if (!jPanel3.isFocusOwner()) {
                jPanel3.requestFocus();


            }
            if ((ix != (int) Math.floor(evt.getPoint().x / 40)) || (iy != (int) Math.floor(evt.getPoint().y / 40))) {

                drawPreview((int) Math.floor(evt.getPoint().x / 40), (int) Math.floor(evt.getPoint().y / 40), rotation);
                ix = (int) evt.getPoint().x / 40;
                iy = (int) evt.getPoint().y / 40;
                //addConsole(Math.floor(evt.getPoint().x / 40) + "," + Math.floor(evt.getPoint().y / 40));


            }

        }
    }//GEN-LAST:event_jPanel3MouseMoved

    private void jPanel3MouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_jPanel3MouseWheelMoved
        if (evt.getWheelRotation() < 0) {
            rotation = (rotation + 90) % 360;
            addConsole(
                    "rotate" + rotation);
            drawPreview(
                    ix, iy, rotation);



        }

        if (evt.getWheelRotation() > 0) {
            rotation = (rotation - 90) % 360;


            if (rotation < 0) {
                rotation = 360 + rotation;


            }
            addConsole("rotate" + rotation);
            drawPreview(
                    ix, iy, rotation);



        }
    }//GEN-LAST:event_jPanel3MouseWheelMoved

    private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseClicked
        if (selectedfield == 1) {
            jLabel1.setBackground(Color.white);
            selectedfield = 0;


        } else {
            selectedfield = 1;
            jLabel1.setBackground(Color.pink);
            jLabel2.setBackground(Color.white);
            jLabel3.setBackground(Color.white);
            jLabel4.setBackground(Color.white);
            jLabel5.setBackground(Color.white);
            jLabel8.setBackground(Color.white);

        }
    }//GEN-LAST:event_jLabel1MouseClicked

    private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel2MouseClicked
        if (selectedfield == 2) {
            jLabel2.setBackground(Color.white);
            selectedfield = 0;


        } else {
            selectedfield = 2;
            jLabel1.setBackground(Color.white);
            jLabel2.setBackground(Color.pink);
            jLabel3.setBackground(Color.white);
            jLabel4.setBackground(Color.white);
            jLabel5.setBackground(Color.white);
            jLabel8.setBackground(Color.white);

        }
    }//GEN-LAST:event_jLabel2MouseClicked

    private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jTabbedPane1StateChanged
        if (!wookiefree) {
            if (jTabbedPane1.getSelectedIndex() == 1) {

                jLabel1.setBackground(Color.white);
                jLabel2.setBackground(Color.white);
                jLabel3.setBackground(Color.white);
                jLabel4.setBackground(Color.white);
                jLabel5.setBackground(Color.white);
                jMenuItem1.setEnabled(false);
                jMenuItem2.setEnabled(false);
                jMenuItem3.setEnabled(false);
                jMenuItem4.setEnabled(false);
                selectedfield = 0;
                wookiefree = true;
                drawCanvas();
                addConsole(
                        "Simulation Mode started...");


            }

        } else {

            wookiefree = false;
            jMenuItem1.setEnabled(!false);
            jMenuItem2.setEnabled(!false);
            jMenuItem3.setEnabled(!false);
            jMenuItem4.setEnabled(!false);
            wookie = null;


            if (loaded) {
                loaded = false;
                jList1.setEnabled(!false);
                jLabel10.setBackground(Color.white);
                addWookies();
                addConsole(
                        "Algorithm dismounted!");


            }
            drawCanvas();
            addConsole(
                    "Level Editor started...");


        }

    }//GEN-LAST:event_jTabbedPane1StateChanged

    private void jPanel5MouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_jPanel5MouseWheelMoved
    }//GEN-LAST:event_jPanel5MouseWheelMoved

    private void jPanel5MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel5MouseReleased
        drawWookieBrain();
    }//GEN-LAST:event_jPanel5MouseReleased

    private void jPanel5MouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel5MouseMoved
        // TODO add your handling code here:
    }//GEN-LAST:event_jPanel5MouseMoved

    private void jPanel5KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPanel5KeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_jPanel5KeyReleased

    private void jLabel12MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel12MouseClicked
        jFrame1.setVisible(true);
        drawWookieBrain();
    }//GEN-LAST:event_jLabel12MouseClicked

    private void jLabel8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel8MouseClicked
        if (selectedfield == 6) {
            jLabel8.setBackground(Color.white);
            selectedfield = 0;


        } else {
            selectedfield = 6;
            jLabel1.setBackground(Color.white);
            jLabel2.setBackground(Color.white);
            jLabel3.setBackground(Color.white);
            jLabel4.setBackground(Color.white);
            jLabel5.setBackground(Color.white);
            jLabel8.setBackground(Color.pink);

        }
    }//GEN-LAST:event_jLabel8MouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;

    private void loadWookie() {
        if (!loaded) {
            loaded = true;
            setWookieFree(
                    alist.get(jList1.getSelectedIndex()));
            jLabel10.setBackground(Color.pink);
            addConsole(
                    "Algorithm \"" + jList1.getSelectedValue() + "\" loaded...");
            jList1.setEnabled(false);


        } else {
            loaded = false;
            jList1.setEnabled(!false);
            jLabel10.setBackground(Color.white);
            addWookies();
            addConsole(
                    "Algorithm dismounted!");

        }
    }
}
